package com.dll.pybig.controller;


import com.dll.pybig.pojo.StuScore;
import com.dll.pybig.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ScoreController {
    @Autowired
    ScoreService scoreService;

    @PostMapping(value = {"/infos/show/{pages}", "/infos/show"})
    public List<StuScore> getStudentScore(@RequestParam("stuName") String stuName, @PathVariable(required = false) Integer pages) {
        if (pages == null) {
            pages = 1;
        }
        return scoreService.getAllStuScore(stuName, pages);

    }

    @PostMapping(value = {"/infos/shows/{pages}", "/infos/shows"})
    public Object getIpage(@RequestParam("stuName") String stuName, @PathVariable(required = false) Integer pages) {
        if (pages == null) {
            pages = 1;
        }
        return scoreService.getAllStuScoreIpa(stuName, pages);
    }

    @PostMapping("/stu/add")
    public Integer addPerson(String stuNum, String stuName,
                             String signUpTimes, String averSeat, String averSignScore,
                             String askScore,
                             String totalScore) {
        StuScore stuScore = new StuScore(Long.valueOf(stuNum), stuName, Long.valueOf(signUpTimes), Double.valueOf(averSeat),
                Double.valueOf(averSignScore), Double.valueOf(askScore), Double.valueOf(totalScore));
        Integer ans = scoreService.insertStuScore(stuScore);
        return ans;
    }

    @PostMapping("/stu/get")
    public StuScore getPersonById(String stuNum) {

        Long num = Long.valueOf(stuNum);
        return scoreService.getStuScoreByStuNums(num);
    }


    @PostMapping("/stu/update")
    public Integer updateByStuNum(String stuNum, String stuName,
                                  String signUpTimes, String averSeat, String averSignScore,
                                  String askScore,
                                  String totalScore) {
        StuScore s = scoreService.getStuScoreByStuNums(Long.valueOf(stuNum));
        s.setStuName(stuName);
        s.setSignupTimes(Long.valueOf(signUpTimes));
        s.setAvgSeatNum(Double.valueOf(averSeat));
        s.setSignUpScore(Double.valueOf(averSignScore));
        s.setAskScore(Double.valueOf(askScore));
        s.setTotalScore(Double.valueOf(totalScore));

        return scoreService.updateByStuNums(s, Long.valueOf(stuNum));
    }


    @PostMapping("/stu/delete")
    public Integer deleteUser(String stuNum) {
        Integer integer = scoreService.deletedByStuNums(Long.valueOf(stuNum));
        return integer;
    }

    @PostMapping("/stu/getDeleted")
    public Object getDeleted(@RequestParam("stuName") String stuName, @PathVariable(required = false) Integer pages) {
        if(pages==null){
            pages = 1;
        }
        return scoreService.selectDeleted(stuName, pages);

    }

    @PostMapping("/stu/recoverUser")
    public Integer recoverUser(@RequestParam("stuNum") String stuNums) {
        return scoreService.recoverUser(Long.valueOf(stuNums));
    }


    @GetMapping("/info/reload")
    public Integer reloadUser(){
       return    scoreService.getDataReloaded();
    }

}
